//
//  YJHBViewController.m
//  Estate
//
//  Created by kevin on 16/9/5.
//  Copyright © 2016年 ttsx. All rights reserved.
//
#define margin 15

#import "YJHBViewController.h"
#import "DefaineCell.h"
#import "DoubleLabCell.h"
#import "B_wuLiaoCell.h"
#import "ZJRWViewController.h"
#import "LeaderYijianCell.h"
#import "addDiaryHeadView.h"
@interface YJHBViewController ()

@end

@implementation YJHBViewController
//懒加载
- (NSMutableArray *)dataArray{
    if(_dataArray==nil){
        _dataArray=[NSMutableArray array];
    }
    return  _dataArray;
}


-(TPKeyboardAvoidingTableView *)tableView{
    
    if (!_tableView) {
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces = NO;
        _tableView.showsVerticalScrollIndicator =
        NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"DoubleLabCell" bundle:nil] forCellReuseIdentifier:@"dCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"sCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"LeaderYijianCell" bundle:nil] forCellReuseIdentifier:@"detail"];
        
    }
    return _tableView;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"一键合并";
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self.view addSubview:self.tableView];
    
    [self setData];
}

#pragma mark- 设置数据
- (void) setData{
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    
    [db open];
    
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM INSPECT_PROCESS_RECORD WHERE INSPECT_PROCESS_ID = '%@'",self.processID];
    
    NSLog(@"%@",sqlStr);
    
    FMResultSet *resul = [db executeQuery:sqlStr];
    
    while ([resul next]){
        
        NSString *time = [resul stringForColumn:@"CREATE_TIME"];
        
        self.PRO_ID = [resul stringForColumn:@"PRO_ID"];
        
        self.ITEM_ID = [resul stringForColumn:@"INSPECT_ITEM_ID"];
        
        NSDictionary *dics = @{@"time":time,@"PRO_ID":self.PRO_ID,@"ITEM_ID":self.ITEM_ID};
        
        [self.dataArray addObject:dics];
        
        
        
    }
    
    NSDictionary *dic=@{@"name":@"王五",@"state":@"1",@"info":@"设计变更、洽商是否在图纸上"};
    NSMutableArray *arr=[NSMutableArray arrayWithObjects:dic ,nil];
    NSDictionary *dic_1=@{@"title":@"质量控制",@"data":arr};
    [self.dataArray addObject:dic_1];
    
    NSDictionary *dic1=@{@"name":@"赵六",@"state":@"0",@"info":@"工程延期是否按流程要求办理"};
    NSMutableArray *arr1=[NSMutableArray arrayWithObjects:dic1,nil];
    NSDictionary *dic_2=@{@"title":@"进度控制",@"data":arr1};
    [self.dataArray addObject:dic_2];
    
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count+2;
}


//返回组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //得到类别下content
    if(section==0)//第一组
    {
        return 5;
    }else if(section==self.dataArray.count+1)//倒数第一组
    {
        return 1;
    }
    else//查询动态添加组
    {
        NSDictionary *dict=self.dataArray[section-1];
        NSMutableArray *arr=[dict objectForKey:@"data"];
        return arr.count;
    }
    
}
//组尾
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    //第一组
    if(section==0||section==self.dataArray.count+1){
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH-20, 15)];
        view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
        //波浪线
        UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
        img.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
        img.image=[UIImage imageNamed:@"juchishang"];
        [view addSubview:img];
        return view;
        
    }else{
        //横线
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1)];
        line.backgroundColor=[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
        return line;
    }
}
//组尾高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section==0){
        return 15;
    }else
        return 1;
}

//组头视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //第一组 最后一组
    if(section==0 ||section==self.dataArray.count+1)
    {
        return nil;
        
    }else if(section==1)
        
    {
        addDiaryHeadView *headView=[[addDiaryHeadView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 30)];
        headView.title.text=self.dataArray[section-1][@"title"];
        return headView;
        
    }
    else {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 25)];
        UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(8, 8, DEF_SCREEN_WIDTH, 15)];
        titleLab.font=[UIFont systemFontOfSize:15.];
        view.backgroundColor=[UIColor whiteColor];
        titleLab.textColor=[UIColor colorWithRed:0.47 green:0.48 blue:0.49 alpha:1.00];
        titleLab.text=self.dataArray[section-1][@"title"];
        [view addSubview:titleLab];
        return view;
    }
    
}

//组头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if(section==0||section==self.dataArray.count+1){
        return 0.1;
    }else if (section==1){
        return 30.;
    }else
        return 25;
}
//每行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section==self.dataArray.count+1)
    {
        return 120;
    }
    else
    {
        return 44;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //  WEAKSELF
    //第一组
    if(indexPath.section==0){
        if(indexPath.row==0){
            
            DoubleLabCell  *cell=[tableView dequeueReusableCellWithIdentifier:@"dCell" forIndexPath:indexPath];
            
            cell.backgroundColor=[UIColor colorWithRed:0.85 green:0.93 blue:0.97 alpha:1.00];
            //标题
            NSString *title=[NSString stringWithFormat:@">>%@ %@ 巡检任务",@"",@""];
            //审阅否
            NSString *state;
                        if([state isEqualToString:@"0"]){
            state=@"状态:未提交";
                        }else{
                            state=@"状态:已审阅";
                        }
            
            if([self.flag isEqualToString:@"add"]){
                [cell setTitle:title AndTitleColor:[UIColor colorWithRed:0.24 green:0.64 blue:0.61 alpha:1.00] AndDetail:@"" AndDetailColor:[UIColor colorWithRed:0.88 green:0.35 blue:0.42 alpha:1.00]];
            }else{
                [cell setTitle:title AndTitleColor:[UIColor colorWithRed:0.24 green:0.64 blue:0.61 alpha:1.00] AndDetail:state AndDetailColor:[UIColor colorWithRed:0.88 green:0.35 blue:0.42 alpha:1.00]];
            }
            
            return cell;
            
        }else{
            
            if(indexPath.row==1){
                
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                //根据id 查询项目名
                //                NSString *proname=[LSUtility  getProjectNameByProjectId:_model.PRO_ID][@"PROJECT_NAME"];
                //
                                NSString *str = [LSUtility getProjectNameByProjectId:self.ITEM_ID][@"PARENT_ID"];
                
                [cell setTitle:@"项    目:" andDetail:str andLeftConstraint:8];
                
                return cell;
                
            }else if (indexPath.row==2){
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                
                [cell setTitle:@"记 录 人:" andDetail:@"" andLeftConstraint:8];
                
                return cell;
            }if (indexPath.row==3){
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                
                                NSString *str = [self.dataArray[0] objectForKey:@"time"];
                
                [cell setTitle:@"时    间:" andDetail:str andLeftConstraint:8];
                
                return cell;
                
            }else{
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                
                //                NSString *diqu=[[_model.WEATHER componentsSeparatedByString:@"/"]firstObject];
                
                [cell setTitle:@"天    气:" andDetail:@"" andLeftConstraint:8];
                
                return cell;
                
            }
        }
    } else if(indexPath.section==self.dataArray.count+1)//最后一组
    {
        B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"sCell" forIndexPath:indexPath];
        cell.isEdit=self.isEdit;
        cell.textValueChangedBlock=^(NSString *value){
        };
        
        [cell setTitle:@"说   明:" andPlaceHolder:@"请输入说明信息" andTextValue:@""];
        
        return cell;
        
    }
    else {
        
        LeaderYijianCell *cell=[LeaderYijianCell cellWithTableView:tableView];
        cell.isEdit=self.isEdit;
        
        NSDictionary *dict=self.dataArray[indexPath.section-1];
        NSMutableArray *arr=[dict objectForKey:@"data"];
        NSDictionary *cellDataDic=arr[indexPath.row];
        
        cell.titleField.text=cellDataDic[@"info"];
        //执行编辑操作
        cell.textValueChangedBlock=^(NSString *value){
            //重新设置标题
            [cellDataDic setValue:value forKey:@"info"];
        };
        //查看页面
        cell.deleteBtn.hidden=YES;
        cell.leftConstraint.constant=-25.*BIZ;
        
        //详情信息
        cell.detailBlick=^{
            ZJRWViewController *zjCtr=[[ZJRWViewController alloc]init];
            //leader.isEdit=NO;
            [self.navigationController pushViewController:zjCtr animated:YES];
        };
        return cell;
    }
    
}


@end
